;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.UreportPreview", [])
        .controller("UreportPreviewCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", function ($scope, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout) {
            $scope.markInfo = {
                curRow: 0
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };

            $scope.condition = {};
            $scope.dataList = [];

            $scope.tableConfig = {
                fixedWidth: 60,
                scrollWidth: 600,
                tableCol: [
                    {
                        name: "文件名",
                        dataField: "reportname",
                        width: 300,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "创建时间",
                        dataField: "createDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "更新时间",
                        dataField: "modifiedDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "操作",
                        isShow: true,
                        isFixed: true,
                    }
                ]
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/ureportManage/listAllPage";
                var param = {};
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageSize = $scope.pageInfo.pageSize;
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createDateShow = $filter("YbDate")(item.createDate, 'yyyy-MM-dd HH:mm:ss');
                        item.modifiedDateShow = $filter("YbDate")(item.modifiedDate, 'yyyy-MM-dd HH:mm:ss');
                        item.url = $scope.hostOrigin + "/dev/ureport/preview?_u=" + item.previewReportname;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {scrollLeft: {left: $scope.tableConfig.fixedWidth + "px"}};
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show"+ toolbar]) {
                    $scope.toolbarConfig["show"+ toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show"+ toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.ureportDesignerUrl = window.location.origin + "/dev/ureport/designer";

            $scope.previewItem = function (url) {
                window.open(url, '_blank');
            };

            (function () {
                if (!window.location.origin) {
                    $scope.hostOrigin = window.location.protocol + '//' + window.location.host;
                } else {
                    $scope.hostOrigin = window.location.origin;
                }
                getDataList();
                $scope.setFixedWidth();
            })();
        }])
    ;
})(window, window.angular);